package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.knowledge.base.pojo.CountPojo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.KnowledgeTagDao;
import com.xbongbong.saas.domain.entity.KnowledgeTagEntity;
import com.xbongbong.saas.enums.TagParentEnum;
import com.xbongbong.saas.model.KnowledgeTagModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author bomfish
 */
@Service("knowledgeTagModel")
public class KnowledgeTagModelImpl implements KnowledgeTagModel {
    @Resource
    private KnowledgeTagDao knowledgeTagDao;

    @Override
    public Integer insert(KnowledgeTagEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return knowledgeTagDao.insert(entity);
    }

    private void timeSet(List<KnowledgeTagEntity> list) {
        long now = DateTimeUtil.getInt();
        list.forEach(o -> {
            o.setAddTime(now);
            o.setUpdateTime(now);
        });
    }

    @Override
    public void insertBatch(List<KnowledgeTagEntity> list) {
        timeSet(list);
        int size = list.size();
        int page = (size - 1) / 1000;
        for (int currPage = 0; currPage <= page; currPage++) {
            int start = currPage * 1000;
            int end;
            if (size - start < 1000) {
                end = size;
            } else {
                end = start + 1000;
            }
            knowledgeTagDao.insertBatch(list.subList(start, end));
        }
    }

    @Override
    public List<KnowledgeTagEntity> findEntitys(Map<String, Object> param) {
        return knowledgeTagDao.findEntities(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return knowledgeTagDao.getEntitiesCount(param);
    }

    /**
     * Description: 查询符合条件的数据（无子表单数据）
     *
     * @param param
     * @return java.util.List<T>
     * @throws
     * @author 魏荣杰
     * @date 2020/10/13 16:07
     * @since
     */
    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer update(KnowledgeTagEntity entity) {
        return knowledgeTagDao.update(entity);
    }

    @Override
    public KnowledgeTagEntity getByKey(Long key, String corpid) {
        return knowledgeTagDao.getByKey(key, corpid);
    }

    @Override
    public Map<Long, KnowledgeTagEntity> getTagDataMap(Set<Long> tagList, String corpid) {
        Map<String, Object> param = new HashMap<>();
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("parentType", TagParentEnum.CHILD.getCode());
        if (Objects.nonNull(tagList) && !tagList.isEmpty()) {
            param.put("tagIdIn", tagList);
        }
        List<KnowledgeTagEntity> list = findEntitys(param);
        return list.stream().collect(Collectors.toMap(KnowledgeTagEntity::getId, Function.identity()));
    }

    @Override
    public Map<Long, String> getParentTagMap(String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("parentType", TagParentEnum.PARENT.getCode());
        List<KnowledgeTagEntity> list = findEntitys(param);
        return list.stream().collect(Collectors.toMap(KnowledgeTagEntity::getId, KnowledgeTagEntity::getName));
    }

    @Override
    public List<KnowledgeTagEntity> getByTagIdList(List<Long> tagIdList, String corpid) {
        return knowledgeTagDao.getByTagIdList(tagIdList, corpid);
    }

    /**
     * 移动到回收站
     *
     * @param idList -
     * @param corpid -
     */
    @Override
    public void move2RecycleBin(List<Long> idList, String corpid) {
        if (CollectionsUtil.isEmpty(idList)){
            return;
        }
        knowledgeTagDao.move2RecycleBin(idList, corpid);
    }

    @Override
    public void deleteById(Long id, String corpid) {
        knowledgeTagDao.deleteById(id, corpid);
    }

    @Override
    public void deleteByIdList(List<Long> idList, String corpid) {
        knowledgeTagDao.deleteByIdList(idList, corpid);
    }

    @Override
    public void sort(Map<String, Object> param, Map<Long, Long> sortMap) {
        knowledgeTagDao.sort(param, sortMap);
    }

    @Override
    public void editSameCount(List<String> nameList, String corpid) {
        List<CountPojo> countPojoList = knowledgeTagDao.getSameCount(nameList, corpid);
        if (CollectionsUtil.isEmpty(countPojoList)) {
            return;
        }
        knowledgeTagDao.editSameCount(countPojoList, corpid);
    }
}
